using System;
using System.Collections;
using System.Web;
//using Sedna.Core.Common.Expression;
using Sedna.Core.Common.Objects.Navigation;
using Sedna.Core.Globalization;
using Sedna.Core.Security;
using log4net;

namespace Sedna.Core.Controller.Common
{
	public abstract class AbstractAction : IAction
	{
	    
        protected IScopePathParser scopePathParser; 
		protected string successStatus = "success";
		protected IDictionary statuses = new Hashtable();
		protected IList actionParams = new ArrayList();
		protected int errorCode = 0;
		protected IPrincipalWrapper principalWrapper;
		protected ISessionWrapper sessionWrapper;
        protected PostBackCondition postBackCondition = PostBackCondition.Always;

        #region Properties

        public PostBackCondition PostBackCondition
	    {
            set { postBackCondition = value; }
	    }

	    public IScopePathParser ScopePathParser
	    {
	        set { scopePathParser = value; }
	    }

	    public ISessionWrapper SessionWrapper
		{
			set { sessionWrapper = value; }
		}

		public string SuccessStatus
		{
			set { successStatus = value; }
		}

		public IDictionary Statuses
		{
			set { statuses = value; }
		}

		public virtual IList ActionParams
		{
			get { return actionParams; }
			set { actionParams = value; }
		}

		public int ErrorCode
		{
			get { return errorCode; }
			set { errorCode = value; }
		}

		public IPrincipalWrapper PrincipalWrapper
		{
			set { principalWrapper = value; }
        }


	    public bool HasErrors
	    {
            get { return errorCode != 0; }
	    }
        #endregion

        public AbstractAction()
		{
		}

		public abstract IActionResult Execute(IExecutionContext executionContext);

	    public virtual bool CanExecute(IExecutionContext executionContext)
	    {
	        if (postBackCondition==PostBackCondition.Always)
	        {
                return true;
	        }      
	        if (executionContext.IsPostBack&&postBackCondition==PostBackCondition.PostBack)
	        {
                return true;    
	        }
	        if (!executionContext.IsPostBack&&postBackCondition==PostBackCondition.NotPostBack)
	        {
                return true;
	        }
            return false;
	    }
	    

		protected ActionResult CreateResult()
		{
			return new ActionResult(successStatus);
		}

		protected ActionResult CreateResult(string successStatus)
		{
            if (successStatus == null || successStatus == string.Empty 
                || ((statuses ==null||statuses.Count==0)&&successStatus.Equals("success")))
			{
				return CreateResult();
			}
			string returnStatus = statuses[successStatus] as string;
			return new ActionResult(returnStatus!=null?returnStatus:successStatus);
		}
	    
	    protected object GetValueByPath(string path)
	    {
            return scopePathParser.Parse(path).Value;
	    }
	    
	    protected void SetValueByPath(string path, object value)
	    {
            scopePathParser.Parse(path).Value = value;
	    }
	}

}
